

import { SelfDefFundAccountType } from "./selfDefFundAccountType"
import { CsDouble } from '../common/csDouble.js';
import { Util } from "../../utils/util.js";

export class TransactionBalance {

    _metalFundType = null;      // SelfDefFundAccountType
    _subCommodityId = null;

    _balance = CsDouble.zero();  // 总账余额

    // Constructor
    constructor(
        {
            metalFundType,
            subCommodityId,
            balance
        } = {}) {

        this._metalFundType = metalFundType;
        this._subCommodityId = subCommodityId;
        this._balance = balance || CsDouble.zero();
    }


    get metalFundType() {
        return this._metalFundType;
    }

    set metalFundType(value) {
        this._metalFundType = value;
    }

    get subCommodityId() {
        return this._subCommodityId;
    }

    set subCommodityId(value) {
        this._subCommodityId = value;
    }

    get balance() {
        return this._balance;
    }

    set balance(value) {
        this._balance = value;
    }


    // 克隆方法
    static clone(unit) {
        return new TransactionBalance(
            {
                metalFundType: unit._metalFundType,
                subCommodityId: unit._subCommodityId,
                balance: unit._balance ? new CsDouble(unit._balance.value) : CsDouble.zero(),
            }
        );
    }

    // 客户贵金属统计
    static fromJson(json) {
        let item = new TransactionBalance(
            {
                metalFundType: json.metalFundTypeId ? new SelfDefFundAccountType(json.metalFundTypeId) : null,
                subCommodityId: Util.jsonToString(json.specId),
                balance: Util.jsonToCsFundDouble(json.balance, 0),
            }
        );
        console.log('TransactionBalance fromJson:',json,item );
        return item;
    }
}